<?php


namespace App\Http\Controllers\Mini\Shop;


use App\Http\Controllers\Controller;
use App\Services\FavoriteService;
use App\Services\UserService;
use Illuminate\Http\Request;

/**
 * 用戶收藏关注控制器
 * Class FavoritesController
 * @package App\Http\Controllers\Mini
 */
class FavoritesController extends Controller
{
    public $favoritesModel;

    public function __construct()
    {
        $this->favoritesModel = new FavoriteService();
    }


    /**
     * 获取收藏或关注列表
     * @return array
     */
    public function getFavoritesList()
    {
        try {
            return $this->favoritesModel->getFav();
        } catch (\Exception $e) {
            return $this->format_error('请求失败' . $e->getMessage());
        }
    }

    /**
     * 添加收藏或关注
     * @param Request $request
     * @return array
     */
    public function addFavorites(Request $request)
    {
        try {
            return $this->favoritesModel->addFav($request->fav_id);
        } catch (\Exception $e) {
            return $this->format_error('添加失败' . $e->getMessage());
        }
    }


    /**
     * 2021/3/18--9:44
     * @param Request $request
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:isFav
     * @effect:是否收藏
     */
    public function isFav(Request $request)
    {
        try {
            return $this->favoritesModel->isFav($request->fav_id);
        } catch (\Exception $e) {
            return $this->format_error('添加失败' . $e->getMessage());
        }
    }

    /**
     * 删除收藏或关注
     * @param Request $request
     * @return array
     */
    public function delFavorites(Request $request)
    {
        try {
            $id = $request->fav_id;
            $idArray = array_filter(explode(',', $id), function ($item) {
                return is_numeric($item);
            });
            return $this->favoritesModel->delFav($idArray);
        } catch (\Exception $e) {
            return $this->format_error('删除失败' . $e->getMessage());
        }
    }


    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->favoritesModel;
    }
}
